package com.heu.blood.autologous.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.autologous.dictenum.ApplicationStateAutologousEnum;
import com.heu.blood.autologous.dictenum.BloodStateAutologousEnum;
import com.heu.blood.autologous.entity.BloodApplicationItemAutologousEntity;
import com.heu.blood.common.Constant.ConstantData;
import com.heu.blood.common.utils.GenerateNumberBySnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.autologous.dao.BloodInformationAutologousDao;
import com.heu.blood.autologous.entity.BloodInformationAutologousEntity;
import com.heu.blood.autologous.service.BloodInformationAutologousService;


@Service("bloodInformationAutologousService")
public class BloodInformationAutologousServiceImpl extends ServiceImpl<BloodInformationAutologousDao, BloodInformationAutologousEntity> implements BloodInformationAutologousService {


    @Autowired
    private BloodInformationAutologousDao bloodInformationAutologousDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BloodInformationAutologousEntity> page = this.page(
                new Query<BloodInformationAutologousEntity>().getPage(params),
                new QueryWrapper<BloodInformationAutologousEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<BloodInformationAutologousEntity> getPendingBloodInformation() {

        LambdaQueryWrapper<BloodInformationAutologousEntity> informationAutologousEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        informationAutologousEntityLambdaQueryWrapper.eq(BloodInformationAutologousEntity::getBloodState, BloodStateAutologousEnum.AUTO_IN_STOCK);

        List<BloodInformationAutologousEntity> bloodInformationAutologousEntities = bloodInformationAutologousDao.selectList(informationAutologousEntityLambdaQueryWrapper);

        if (bloodInformationAutologousEntities!=null&&!bloodInformationAutologousEntities.isEmpty()){
            return bloodInformationAutologousEntities;
        }else{
            return null;
        }
    }

    @Override
    public boolean addBloodAutologousInformation(BloodInformationAutologousEntity bloodInfo) {
        GenerateNumberBySnowFlake generateNumberBySnowFlake = new GenerateNumberBySnowFlake(ConstantData.AUTOLOGOUS_BLOOD_NO_WORK_ID, ConstantData.AUTOLOGOUS_BLOOD_NO_PREFIX);
        String bloodId = generateNumberBySnowFlake.genNumberByflag();

        bloodInfo.setBloodId(bloodId);
        bloodInfo.setBloodState(BloodStateAutologousEnum.AUTO_IN_STOCK.code());
        int insert = bloodInformationAutologousDao.insert(bloodInfo);
        if (insert==1){
            return true;
        }else{
            return false;
        }

    }
}